package task.timer;

import com.qax.task.timer.core.ScheduledCompletableFuture;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

@State(Scope.Thread)
@BenchmarkMode(Mode.Throughput)
@Warmup(iterations = 3)
@Measurement(iterations = 10, time = 3)
@OutputTimeUnit(TimeUnit.SECONDS)
public class ScheduledBenchmark {


    @Benchmark
    public void test1() {
        ScheduledCompletableFuture.schedule(
                (Supplier<Void>) () -> {
                    int i;
                    for (i = 0; i < 10000000; i++) {
                        i--;
                    }
                    return null;
                }, 0, TimeUnit.SECONDS);
    }

    /**
     * Iteration   1: 1226008.413 ops/s
     * Iteration   2: 689065.746 ops/s
     * Iteration   3: 860038.996 ops/s
     * Iteration   4: 771610.382 ops/s
     * Iteration   5: 1193957.993 ops/s
     * @param args
     * @throws Exception
     */
    public static void main(final String[] args) throws Exception {
        final Options opt = new OptionsBuilder()
                .include(ScheduledBenchmark.class.getSimpleName())
                .jvmArgsAppend("-XX:+UnlockDiagnosticVMOptions")
//                 "-XX:+TraceClassLoading",
//                 "-XX:+LogCompilation",
//                 "-XX:+PrintAssembly")
                .warmupIterations(1)
                .measurementIterations(5)
                .forks(1)
                .build();
        new Runner(opt).run();
    }
}
